package com.ljb.commlibrary.http;

import android.os.Parcel;
import android.os.Parcelable;

import com.ljb.commlibrary.utils.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 网络请求需要的参数列表
 * Created by ljb on 2018/5/2.
 */

public class HttpParams implements Parcelable {
    /**
     * 普通的键值对参数
     */
    private LinkedHashMap<String, String> urlParamsMap;

    public HttpParams() {
        urlParamsMap = new LinkedHashMap<>();
    }

    public void put(String key, String value) {
        if (!StringUtils.isEmpty(key, value)) {
            urlParamsMap.put(key, value);
        }
    }

    public void put(String key, int value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, long value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, float value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, double value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, char value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, boolean value) {
        put(key, String.valueOf(value));
    }

    public void put(HttpParams params) {
        if (params != null && params.getUrlParamsMap() != null && !params.getUrlParamsMap().isEmpty())
            urlParamsMap.putAll(params.getUrlParamsMap());
    }

    public void put(Map<String, String> params) {
        if (params != null && !params.isEmpty())
            for (Map.Entry<String, String> entry : params.entrySet()) {
                put(entry.getKey(), entry.getValue());
            }
    }

    public LinkedHashMap<String, String> getUrlParamsMap() {
        return urlParamsMap;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeSerializable(this.urlParamsMap);
    }

    protected HttpParams(Parcel in) {
        this.urlParamsMap = (LinkedHashMap<String, String>) in.readSerializable();
    }

    public static final Parcelable.Creator<HttpParams> CREATOR = new Parcelable.Creator<HttpParams>() {
        @Override
        public HttpParams createFromParcel(Parcel source) {
            return new HttpParams(source);
        }

        @Override
        public HttpParams[] newArray(int size) {
            return new HttpParams[size];
        }
    };
}
